import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

file_path = r"D:\python charm\01\可视化\销售看板参考\销售明细_12.xlsx"
data = pd.read_excel(file_path)
data = data['产品类别'].value_counts()

# 设置图表颜色和标签
labels = data.index
sizes = data.values
colors = ['#FF6F91', '#FF9671', '#FFC75F']

 # 自定义百分比显示函数
def custom_autopct(pct):
    value = pct * sum(sizes) / 100
    if round(value * 10) % 10 >= 6:
        return f"{round(pct)}%"
    else:
        return f"{np.floor(pct)}%"

# 绘制圆环图
plt.figure(figsize=(10, 10))
ax = plt.gca()
ax.set_facecolor('#2E4057')
plt.gcf().set_facecolor('#2E4057')

wedges, texts, autotexts = plt.pie(
    sizes,
    labels=labels,
    autopct=custom_autopct,
    startangle=90,
    colors=colors,
    wedgeprops={'width': 0.4, 'edgecolor': '#2E4057', 'linewidth': 1},
    textprops={'fontsize': 20, 'color': 'white'}
)

# 调整百分比的字体大小
for autotext in autotexts:
    autotext.set_fontsize(30)
    autotext.set_color('white')

# 添加中心的总销量信息
total_sales = sum(sizes)
plt.text(0, 0, f"{total_sales}\n总销", ha='center', va='center', fontsize=30, weight='bold', color='white')

# 添加图例
plt.legend(
    wedges,
    labels,
    loc="center right",
    fontsize=12,
    frameon=False,
    labelcolor='white',
    bbox_to_anchor=(1.2, 0.5)
)

# 设置标题
plt.title('产品类别销量', fontsize=20, color='white')

# 调整布局避免重叠
plt.tight_layout()

plt.savefig(r'D:\python charm\01\可视化\销售看板参考\图片\产品销量.png')

plt.show()